<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    (function (){
        function Observer(){
            this.obj={}
            this.on=function(type,handler){
                this.obj[type]=this.obj[type] || []
                this.obj[type].push(handler)
            }
            this.emit=function(type,msg){
                if(this.obj[type]){
                    this.obj[type].forEach((item)=>{
                        item(msg)
                    })
                }
            }
        }
        window.Observer=Observer
    })()
    //一个订阅者订阅了一条消息
    var os=new Observer
    os.on('李佳琪',function(msg){
        console.log('莉莉收到了来自李佳琪的消息：'+msg);
    })
    os.emit('李佳琪','一起买买买') 

    //多个订阅者订阅了一条消息
    os.on('李佳琪',function(msg){
        console.log('丽丽收到了来自李佳琪的消息：'+msg);
    })
    os.on('李佳琪',function(msg){
        console.log('李丽收到了来自李佳琪的消息：'+msg);
    })
    os.on('李佳琪',function(msg){
        console.log('李立收到了来自李佳琪的消息：'+msg);
    })
    os.emit('李佳琪','一起买买买')

    //没有订阅者订阅消息
    os.emit('李佳琪','一起买买买') 



</script>
</html>